<template>
  <div class="autumn-container">
    <!-- 面包 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>我的抄送</el-breadcrumb-item>
    </el-breadcrumb>

    <div class="autumn-center">
      <!-- 搜索栏 -->
      <div class="query-form">
        <autumn-search :modelValue="queryInfo" @search="search" @reset="resetSearch">
          <template #default>
            <el-form-item prop="processInsName" label="审核事项">
              <el-input v-model="queryInfo.processInsName" />
            </el-form-item>
            <el-form-item prop="name" label="审核节点">
              <el-input v-model="queryInfo.name" />
            </el-form-item>
            <el-form-item prop="assignee" label="审核人">
              <el-input v-model="queryInfo.auditor" />
            </el-form-item>
            <el-form-item label="审核时间">
              <el-date-picker v-model="startEndTime" type="daterange" start-placeholder="开始日期" end-placeholder="结束日期" />
            </el-form-item>
          </template>
        </autumn-search>
      </div>

      <!-- 数据表格 -->
      <div class="main-body top">
        <autumn-table ref="mainTable" :page="page" @changePage="changePage">
          <template #column>
            <el-table-column type="index" fixed="left" label="#" align="center">
              <template #default="scope">
                {{ initIndex(scope.$index) }}
              </template>
            </el-table-column>
            <el-table-column prop="processInsName" label="审核事项" />
            <el-table-column prop="name" label="审核节点" />
            <el-table-column prop="vars.assigneeName" label="审核人" />
            <el-table-column label="审核意见" width="80" align="center">
              <template #default="scope">
                <el-tag size="small" :type="stateColor[scope.row.vars.agree]">
                  {{ $getDictLabel('FLOW_STATUS', scope.row.vars.agree, '') }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="auditTime" label="审核时间" width="150" align="center"
              :formatter="(row: any) => $moment(row.auditTime, 'YYYY-MM-DD HH:mm')" />
            <el-table-column prop="duration" label="审核历时" width="100" align="center"
              :formatter="(row: any) => $convertTimeUnit(row.duration)" />
            <el-table-column prop="vars.opinion" label="备注" />
            <el-table-column label="操作" fixed="right" width="100">
              <template #default="scope">
                <el-button-group>
                  <el-button type="info" icon="el-icon-view" size="small" title="查看" @click="toView(scope.row)" />
                  <el-button type="info" icon="el-icon-tickets" size="small" title="表单" v-if="scope.row.formKey"
                    @click="toForm(scope.row)" />
                </el-button-group>
              </template>
            </el-table-column>
          </template>
        </autumn-table>
      </div>
    </div>

    <!-- 查看弹窗 -->
    <view-task ref="viewTaskRef" />
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, watch } from 'vue'
import { useRouter } from 'vue-router'
import ViewTask from '../../component/ViewTask.vue'
import { pageCCTask } from 'src/api/flow/activiti'
import useTable from 'src/hooks/useTable'
import { $moment, $convertTimeUnit, $getDictLabel } from 'src/hooks/useCommon'
import useFlow from '../../hooks/useFlow'

const { page, initIndex } = useTable()
const { stateColor } = useFlow()

const viewTaskRef = ref()

// 分页查询
const queryInfo = reactive({
  processInsName: '',
  name: '',
  auditor: '',
  startTime: '',
  endTime: ''
})
const changePage = function (pageNumber: number, pageSize: number) {
  page.pageNumber = pageNumber
  page.pageSize = pageSize
  pageCCTask({ ...queryInfo, pageNumber, pageSize }).then(({ data }) => {
    page.data = data.list
    page.total = data.total
  })
}
const search = function () {
  changePage(1, 10)
}
search()

// 监听
const startEndTime = ref([])
watch(startEndTime, (newVal) => {
  if (!newVal || newVal.length === 0) {
    queryInfo.startTime = ''
    queryInfo.endTime = ''
    return
  }
  queryInfo.startTime = $moment(newVal[0])
  queryInfo.endTime = $moment(newVal[1])
})

// 重置
const resetSearch = () => {
  startEndTime.value = []
  queryInfo.startTime = ''
  queryInfo.endTime = ''
}

// 查看历史
const toView = (row: any) => {
  viewTaskRef.value.open(row.processInsId)
}

// 查看表单
const router = useRouter()
const toForm = (row: any) => {
  const { id: taskId, businessKey, processInsId, formKey } = row
  router.push({
    path: formKey,
    query: {
      method: 'view',
      taskId,
      businessKey,
      processInsId
    }
  })
}
</script>

<style lang="scss" scoped>
</style>
